package com.activenetwork.smlite.page.event;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.activenetwork.custom.uielement.UIElement;
import com.activenetwork.custom.uielement.decorate.InJson;
import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.smlite.page.event.SmartFolder.FOLDER_LEVEL;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.page.event.EventDetailView.EventSummarySubview;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.grid.NoMatchRowException;
import com.activenetwork.util.grid.OperatableGrid;
import com.activenetwork.util.grid.condition.ConditionBuilder;
import com.activenetwork.util.grid.condition.EqualCondition;
import com.activenetwork.util.grid.gson.GridFactory;

public class EventsHomeView extends ModelPage {

	@InJson
	private UIElement searchEventInput;
	
	@InJson
	private UIElement searchButton;
	
	@InJson
	private UIElement addEvent;
	
	@InJson
	private UIElement deleteEvent;
	
	@InJson
	private UIElement copyEvent;
	
	private final OperatableGrid grid = GridFactory.getGrid("EventsHomeView", "SearchEventGrid");
	

	/**
	 * Input the search event content
	 * @param eventTitle
	 * @return
	 */
	public EventsHomeView setSearchEvent(String eventTitle){
		searchEventInput.clear();
		searchEventInput.sendKeys(eventTitle);
		return this;
	}
	/**
	 * Click the search button
	 * @return
	 */
	public EventsHomeView clickSearch(){
		searchButton.click();
		return PageLoader.load(EventsHomeView.class);
	}
	/**
	 * Click one specific event name link from the grid according to event data type
	 * @param event
	 * @return
	 * @throws NoMatchRowException
	 */
	public EventSummarySubview selectEventFromGrid(EventType event) throws RuntimeException{
		ConditionBuilder builder = new ConditionBuilder();
		builder.add(new EqualCondition(), 2, event.getBasic().getTitle());
		try{
			grid.clickRow(2, builder.build());
		}catch(NoMatchRowException e){
			throw new RuntimeException("Nested NoMatchException: " + e.getMessage());
		}
		return PageLoader.load(EventSummarySubview.class);	
		
	}
	/**
	 * Click one specific event name link from the grid according to event title string
	 * @param eventTitle
	 * @return
	 * @throws NoMatchRowException
	 */
	public EventSummarySubview selectEventFromGrid(String eventTitle) throws RuntimeException{
		EventType event = new EventType();
		EventBasicType eventBasic = new EventBasicType();
		eventBasic.setTitle(eventTitle);
		return selectEventFromGrid(event);
	}
	/**
	 * Check on one specific event checkbox from the grid according to the event data type
	 * @param event
	 * @return
	 * @throws NoMatchRowException
	 */
	public EventsHomeView checkEventFromGrid(EventType event) throws RuntimeException{
		ConditionBuilder builder = new ConditionBuilder();
		builder.add(new EqualCondition(), 2, event.getBasic().getTitle());
		try{
			grid.checkOnRow(builder.build());
		}catch(NoMatchRowException e){
			throw new RuntimeException("Nested NoMatchException: " + e.getMessage());
		}
		return PageLoader.load(EventsHomeView.class);	
	}
	/**
	 * Check on one specific event checkbox from the grid according to event title string
	 * @param eventTitle
	 * @return
	 * @throws NoMatchRowException
	 */
	public EventsHomeView checkEventFromGrid(String eventTitle) throws RuntimeException{
		EventType event = new EventType();
		EventBasicType eventBasic = new EventBasicType();
		eventBasic.setTitle(eventTitle);
		return checkEventFromGrid(event);
	}
	/**
	 * Click add event button
	 * @return
	 */
	public EventBasicSubview clickAddEvent(){
		addEvent.click();
		return PageLoader.load(EventBasicSubview.class);
	}
	/**
	 * Click delete event button
	 * @return
	 */
	public EventsHomeView clickDeleteEvent(){
		deleteEvent.click();
		return PageLoader.load(EventsHomeView.class);
	}
	/**
	 * Handling the popup when deleting event, it can be OK for deletion of cancellation
	 * @param ok
	 * @return
	 */
	public EventsHomeView confirmPopup(boolean ok){
		WebDriver driver = EnvContext.webDriverTL.get();
		if(ok){
			driver.findElement(By.cssSelector(".gwt-PopupPanel div.modal-footer button.btn-orange")).click();
		}else{
			driver.findElement(By.cssSelector(".gwt-PopupPanel div.modal-footer button.btn:first-child")).click();
		}
		return PageLoader.load(EventsHomeView.class);
	}
	
	public void clickCopyEvent(){
		copyEvent.click();
	}
	
	public EventsHomeView deleteLevel1Folder(String folderName){
		if(folderName.length() == 0){
			throw new RuntimeException("Check your level1 folder node in xml");
		}
		SmartFolder smartFolder = new SmartFolder(folderName, FOLDER_LEVEL.LEVEL1);
		smartFolder.delete();
		return PageLoader.load(EventsHomeView.class);
	}
	
	public EventsHomeView createCascadingFolder(String level1, String level2, String level3){
		SmartFolder lev1 = null;
		SmartFolder lev2 = null;
		SmartFolder lev3 = null;
		
		if(level1.length() > 0){
			lev1 = SmartFolder.createLevel1Folder(level1);
		}else{
			throw new RuntimeException("Check your level1 folder node in xml");
		}
		if(level2.length() > 0){
			lev2 = lev1.newSubfolder(level2);
		}
		if(level3.length() > 0){
			lev3 = lev2.newSubfolder(level3);
		}
		
		//highlight
		if(lev3 != null){
			lev3.highlight();
		}else if(lev2 != null){
			lev2.highlight();
		}else{
			lev1.highlight();
		}
		
		return PageLoader.load(EventsHomeView.class);
	}
	
	public EventsHomeView hightlightFolder(String folderName, FOLDER_LEVEL level){
		SmartFolder folder = new SmartFolder(folderName, level);
		folder.highlight();
		return PageLoader.load(EventsHomeView.class);
	}
	/**
	 * Delete all the tag that matches the rule
	 * @param tagName
	 * @return
	 */
	public EventsHomeView deleteTag(String tagName){
		while(true){
			try{
				EventTag tag = new EventTag(tagName, "startsWithIgnoreCase");
				tag.delete();
			}catch(RuntimeException e){
				break;
			}
		}
		
		return PageLoader.load(EventsHomeView.class);
	}
		
	
}
